package personal.study.bl;

import java.sql.Connection;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import personal.study.db.DBMaterial;
import personal.study.form.MaterialForm;
import personal.study.util.DBConnection;

public class BLMaterialAction {
	private static Log log = LogFactory.getLog(BLSupplierAction.class);
	
	public MaterialForm findByPrimaryKey(String MaterialCode) throws Exception{
		MaterialForm materialForm = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbMaterial = new DBMaterial();
			materialForm = dbMaterial.findByPriaryKey(con, MaterialCode);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return materialForm;
	}

	public Collection<MaterialForm> queryMaterial(String conditions, int currentPage, int rowsPerPage) throws Exception{
		Collection<MaterialForm> col = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbMaterial = new DBMaterial();
			col = dbMaterial.queryMaterial(con, conditions, currentPage, rowsPerPage);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		
		return col;
	}
	
	public int getCount(String conditions) throws Exception{
		int count = 0;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			count = new DBMaterial().getCount(con, conditions);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return count;
	}
	
	public boolean insert(MaterialForm materialform) throws Exception{
		boolean col = false;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbmaterial = new DBMaterial();
			col = dbmaterial.insert(con, materialform);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		
		return col;
	}
	
	public boolean update(MaterialForm materialform) throws Exception{
        boolean result = false;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbMaterial = new DBMaterial();
			result = dbMaterial.update(con, materialform);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return result;
		}
	
	public void delete(String MaterialCode) throws Exception{
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbMaterial = new DBMaterial();
			dbMaterial.delete(con, MaterialCode);
		}catch(Exception e){
			log.error(e.getMessage());
		}
	}
	
	public Collection<MaterialForm> findByConditions(String conditions) throws Exception{
		Collection<MaterialForm> col = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBMaterial dbMaterial = new DBMaterial();
			col = dbMaterial.findByConditions(con, conditions);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return col;
	}
}
